<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;


class Apply extends Model
{
    public $fillable = ['type','status','student_id','squad_id','trans_squad_id','op_id','sp_id','sp_time','memo','study_jindu','term_year','term_season','week','student_nums'];

    protected $table = 'applys';

    public function student()
    {
        return $this->belongsTo(Student::class);
    }
    public function get($request)
    {
        if(isset($request['section']) && $request['section']>0){
            $where = array();
            if($request['section'] == 1){
                $where[] = array('name','=','一段班');
            }elseif($request['section'] == 2){
                $where[] = array('name','=','二段班');
            }

            $section = DB::table('Class_section_groups')->where($where)->get(['id']);

            $squad = DB::table('squads')->where('section','=',$section[0]->id)->get(['id','name']);

            $data = array();

            foreach($squad as $v){
                $data[] = array('id'=>$v->id,'name'=>$v->name);
            }
            return $data;
        }elseif(isset($request['squad']) && $request['squad']>0){
            $num = DB::table('students')->where('squad_id',$request['squad'])
                ->count();

            $tutor = DB::table('squads as s')
                ->join('admin_users as au','s.class_teacher','=','au.id')
                ->where('s.id',$request['squad'])
                ->get(['au.en_name']);

            $course = DB::table('crousesheets as cs')
                ->join('admin_users as au','cs.teacher_id','=','au.id')
                ->join('crouses as c','c.id','=','cs.crouse_id')
                ->where('cs.squads_id',$request['squad'])->where('cs.status','=',1)
                ->select('cs.start_time','cs.end_time','au.en_name','c.name')
                ->get();

            $html = '';
            $html .= '班级人数：'.$num.'<br />';
            $html .= '班主任：'.$tutor[0]->en_name.'<br />';

            foreach($course as $v){
                $html .= $v->name.'-老师：'.$v->en_name.'-上课时间：'.$v->start_time.'-'.$v->end_time.'<br />';
            }
            return $html;
        }
    }

    public function submit($request)
    {

    }

    public function squad()
    {
        return $this->belongsTo(Squad::class,'squad_id'); 
    }
}